package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.p2p.bean.AutoBidAuthBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.bean.enums.OssidEnum;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * Created by xx on 2017/7/20.
 * 自动签约
 */
public class AutoBidSignActiviy extends BaseActivity implements View.OnClickListener, InitializeView {
    private TextView tv_bid_sign_phone;
    private HSEditText et_vcode;
    private ValidateButton btn_vcode;
    private HSButton btn_auto_bid_sign;
    private RequestPresenter mPresenter;
    private String zwgt_data;
    private AutoBidAuthBean mAutoBidAuthBean;
    private TextView tv_protocol_auto_bid;
    private CheckBox ck_agree;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_autobidsign;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();

        toolbar_title.setText("自动投标签约");
        mPresenter = new InitializePresenterImpl(mContext, this);
        tv_bid_sign_phone.setText(getString(R.string.send_vcode_to_phone,CommTool.encodPhone(sharedPreferences.getShareProf(UserInfo.BANKMOBILE))));
        btn_vcode.setOnClickListener(this);
        btn_auto_bid_sign.setOnClickListener(this);
        tv_protocol_auto_bid.setOnClickListener(this);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    public void findView() {
        tv_bid_sign_phone = findView(R.id.tv_bid_sign_phone);
        et_vcode = findView(R.id.et_vcode);
        btn_vcode = findView(R.id.btn_vcode);
        btn_auto_bid_sign = findView(R.id.btn_auto_bid_sign);
        tv_protocol_auto_bid = findView(R.id.tv_protocol_auto_bid);
        ck_agree = findView(R.id.ck_agree);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_vcode://短信验证码
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map3 = new JSONObject();

                try {
                    map3.put("srvtxcode", "autoBidAuthPlus");
                    map3.put("phone", sharedPreferences.getShareProf(UserInfo.MOBILE));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams3 = CommTool.getJSONParams(map3, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_SMS_APPLY, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, Constants.EVENT_DATA,
                        jsonParams3, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, sharedPreferences
                                .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
                break;

            case R.id.btn_auto_bid_sign://投标签约
                String vcode = et_vcode.getText().toString();
                if (vcode.isEmpty()) {
                    showToast(getString(R.string.empty_vcode));
                    return;
                }

                if (!RegularUtils.isLength(vcode, 6)) {
                    showToast(R.string.error_vcode);
                    return;
                }

                if (!ck_agree.isChecked()){
                    showToast(getString(R.string.tips_agree_protocol,"中网国投自动投标协议"));
                    return;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map2 = new JSONObject();
                try {
                    map2.put("smscode", vcode);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams2 = CommTool.getJSONParams(map2, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_AUTOBIDAUTH, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test)
                        + TaskConstants.TASK_AUTOBIDAUTH, Constants.EVENT_DATA, jsonParams2, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test)
                        + TaskConstants.TASK_AUTOBIDAUTH, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);

                break;

            case R.id.tv_protocol_auto_bid://自动投标协议
                String zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap mHeader = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET,getString(R.string.h5_protocol)+ TaskConstants.TASK_AUTOBID,
                        "", mConfigSP.getShareProf(TaskConstants.TASK_CONFIG),zwgt_data);
                HeaderMap headerMap = new HeaderMap();
                headerMap.setMap(mHeader);

                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, OssidEnum.REGIST_PROTOCOL.getOssLabel());
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, mContext.getString(R.string.root_h5_url_test) + getString(R.string.h5_protocol)+ TaskConstants.TASK_AUTOBID);
                bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER,headerMap);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
                bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                readyGo(WebProtocolActivity.class, bundle);
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_SMS_APPLY:

                break;

            case TaskConstants.TASK_BIDSIGN:
                showToast("签约成功");
                finish();
                break;
            case TaskConstants.TASK_AUTOBIDAUTH:
                mAutoBidAuthBean = (AutoBidAuthBean) data;

                //访问h5进行自动投标最后确认
                String postData = null;
                try {
                    postData = "accountId=" + URLEncoder.encode(mAutoBidAuthBean.data.accountId, "utf-8") + "&acqRes=" + URLEncoder.encode(mAutoBidAuthBean.data.acqRes, "utf-8") +
                            "&bankCode=" + URLEncoder.encode(mAutoBidAuthBean.data.bankCode, "utf-8") + "&channel=" + URLEncoder.encode(mAutoBidAuthBean.data.channel, "utf-8") +
                            "&forgotPwdUrl=" + URLEncoder.encode(mAutoBidAuthBean.data.forgotPwdUrl, "utf-8") + "&instCode=" + URLEncoder.encode(mAutoBidAuthBean.data.instCode, "utf-8") + "&lastSrvAuthCode=" +
                            URLEncoder.encode(mAutoBidAuthBean.data.lastSrvAuthCode, "utf-8") + "&notifyUrl=" + URLEncoder.encode(mAutoBidAuthBean.data.notifyUrl, "utf-8") +
                            "&orderId=" + URLEncoder.encode(mAutoBidAuthBean.data.orderId, "utf-8") + "&retUrl=" + URLEncoder.encode(mAutoBidAuthBean.data.retUrl, "utf-8") + "&seqNo=" +
                            URLEncoder.encode(mAutoBidAuthBean.data.seqNo, "utf-8") + "&sign=" + URLEncoder.encode(mAutoBidAuthBean.data.sign, "utf-8") + "&smsCode=" +
                            URLEncoder.encode(mAutoBidAuthBean.data.smsCode, "utf-8") + "&totAmount=" + URLEncoder.encode(mAutoBidAuthBean.data.totAmount, "utf-8") + "&txAmount=" +
                            URLEncoder.encode(mAutoBidAuthBean.data.txAmount, "utf-8") + "&txCode=" + URLEncoder.encode(mAutoBidAuthBean.data.txCode, "utf-8") +
                            "&txDate=" + URLEncoder.encode(mAutoBidAuthBean.data.txDate, "utf-8") + "&txTime=" + URLEncoder.encode(mAutoBidAuthBean.data.txTime, "utf-8") + "&version=" +
                            URLEncoder.encode(mAutoBidAuthBean.data.version, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, "自动投标签约");
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, mAutoBidAuthBean.url);
                bundle.putString(BaseWebActivity.BUNDLE_KEY_PARAMS, postData);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.POSTDATA.getIntValue());
                bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                readyGoThenKill(WebAutoBidSignActivity.class, bundle);
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {

    }
}
